home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / GNUUCP_2 / SOURCE / UUSNAP.C < prev    next >
C/C++ Source or Header  |  1989-08-02  |  9KB  |  381 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)uusnap.c    1.5 87/09/25";
  3. /* Derived from uusnap.c    5.7 (Berkeley) 10/9/85 */
  4. #endif
  5.  
  6. /*
  7.  *    Uusnap - displays a snapshot of the uucp system.
  8.  *    originally by    RJKing WECo-MG6565 May 83
  9.  *
  10.  *    Obtained from Rick Adams by John Gilmore, Aug '87.
  11.  *    Mods for uuslave by John Gilmore, Aug '87.
  12.  */
  13.  
  14.  
  15. #include <ctype.h>
  16.  
  17. #include "includes.h"
  18. #include "uucp.h"
  19. #include "sysdep.h"
  20.  
  21. #ifdef UNIX
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #ifdef    NDIR
  25. #include "ndir.h"
  26. #else
  27. #include <sys/dir.h>
  28. #endif
  29. #endif UNIX
  30.  
  31. #ifdef VMS
  32. #include <stat.h>
  33. #define time_t unsigned
  34. #define MAXNAMLEN 255
  35. #define index strchr
  36. #define rindex strrchr
  37. #endif VMS
  38.  
  39. #ifdef MAC
  40. #define MAXNAMLEN 255
  41. #endif MAC
  42.  
  43.  
  44. char *index(), *rindex();
  45.  
  46. #define    NSYSTEM    100                /* max # of systems queued */
  47.  
  48. #define    CMDSLEN    5                /* Length of trailer */
  49. #define    DATALEN    5                /* Length of trailer */
  50. #define    XEQTLEN    5                /* Length of trailer */
  51. #define    NUMCTRS    3                /* # file types to count */
  52. #define    CMDTYPE    0                /* Index into scnt.cntr */
  53. #define    DATTYPE    1                /* Index into scnt.cntr */
  54. #define    XEQTYPE    2                /* Index into scnt.cntr */
  55.  
  56. struct    scnt {                    /* System count structure */
  57.         char    name[MAXBASENAME+1];    /* Name of system */
  58.         short    cntr[NUMCTRS];        /* Count */
  59.         char    stst[32];        /* STST Message */
  60.         time_t    locked;            /* If LCK.sys present */
  61.         int    lockpid;        /* LCK  Process ID */
  62.         int    lockchain;        /* system # of next w/lockpid */
  63.         char    justlck;        /* Only here due to LCK file */
  64.         char    printed;        /* We have printed this sys */
  65.         int    st_type;        /* STST Type */
  66.         int    st_count;        /* STST Count */
  67.         time_t    st_lastime;        /* STST Last time tried */
  68.         time_t    st_retry;        /* STST Secs to retry */
  69.          };
  70.  
  71. int    sndx;                    /* Number of systems */
  72. struct    scnt    sys[NSYSTEM];            /* Systems queued */
  73. int    printorder[NSYSTEM];            /* Sordid order */
  74. int xqtisrunning = 0;
  75.  
  76. /* Sort function for qsort */
  77. int
  78. cmpname(a, b)
  79.     char *a, *b;
  80. {
  81.     int i, j;
  82.  
  83.     i = *(int *)a;
  84.     j = *(int *)b;
  85.  
  86.     return strcmp(sys[i].name, sys[j].name);
  87. }
  88.  
  89.  
  90. main(argc, argv)
  91.     int argc;
  92.     char **argv;
  93. {
  94.     register int i, j, nlen = 0;
  95.     int ich;
  96.     time_t    curtime, t;
  97.  
  98.     argv++;
  99.     while (argv[0] && argv[0][0] == '-') {
  100.         switch (argv[0][1]) {
  101.         case 'x':
  102.             debug = atoi(&argv[0][2]);
  103.             break;
  104.         case 'C':
  105.             uuControl = &argv[0][2];
  106.             break;
  107.         default:
  108.             fprintf(stderr, "uusnap: bad arg %s\n", argv[0]);
  109.             exit(1);
  110.         }
  111.         argv++;
  112.     }
  113.  
  114.     if (argv[0]) {
  115.         fprintf(stderr, "usage: uusnap [-xN] [-Cconfigfile]\n");
  116.         exit(1);
  117.     }
  118.  
  119.     read_params(uuControl);
  120.     chdir(Spool);
  121.     scandir("C", CMDSLEN, NULL, CMDTYPE);
  122.     scandir("D", DATALEN, NULL, DATTYPE);
  123.     scandir("X", XEQTLEN, 'X', XEQTYPE);
  124.     getstst();
  125.     time(&curtime);
  126.     for(i=0; i<sndx; ++i) {
  127.         printorder[i] = i;    /* Set up initial print order */
  128.         if((j = strlen(sys[i].name)) > nlen)
  129.             nlen = j;    /* Get max name length */
  130.     }
  131.  
  132.     qsort((char *)printorder, sndx, sizeof (*printorder), cmpname);
  133.     
  134.     for(j=0; j<sndx; ++j) {
  135.         i = printorder[j];    /* Get real entry number */
  136.         if (sys[i].printed) continue;
  137.         t = (sys[i].st_lastime +sys[i].st_retry) - curtime;
  138.  
  139.         /* decide if STST text is worth printing */
  140.         if (-t < ONEDAY*2 && sys[i].st_type == SS_WRONGTIME) {
  141.             sys[i].stst[0] = '\0';
  142.             if (sys[i].cntr[0]+sys[i].cntr[1]+sys[i].cntr[2] == 0)
  143.                 continue;    /* ignore entire line */
  144.         }
  145.  
  146.         printf("%-*.*s ", nlen, nlen, sys[i].name);
  147.         if(sys[i].cntr[CMDTYPE])
  148.             printf("%3.d Cmd%s ", sys[i].cntr[CMDTYPE],
  149.                 sys[i].cntr[CMDTYPE]>1?"s":" ");
  150.         else
  151.             printf("   ---   ");
  152.         if(sys[i].cntr[DATTYPE])
  153.             printf("%3.d Data ", sys[i].cntr[DATTYPE]);
  154.         else
  155.             printf("   ---   ");
  156.         if(sys[i].cntr[XEQTYPE])
  157.             printf("%3.d Xqt%s ", sys[i].cntr[XEQTYPE],
  158.                 sys[i].cntr[XEQTYPE]>1?"s":" ");
  159.         else
  160.             printf("   ---   ");
  161.         if(*sys[i].stst == NULL || sys[i].locked > sys[i].st_lastime) {
  162.             if(sys[i].locked) {
  163.                 printf("*LOCKED");
  164.                 for (ich = sys[i].lockchain;
  165.                      ich >= 0 && ich != i;
  166.                      ich = sys[ich].lockchain) {
  167.                     printf("  %s", sys[ich].name);
  168.                     sys[ich].printed++;
  169.                 }
  170.                 if (sys[i].lockpid)
  171.                     printf("  process %d", sys[i].lockpid);
  172.             }
  173.             printf("\n");
  174.             continue;
  175.         }
  176.         printf(" %s  ", sys[i].stst);
  177.         /* decide if STST info is worth pursuing */
  178.         if (-t < ONEDAY*2 && (sys[i].st_count == 0
  179.           || sys[i].st_type == SS_WRONGTIME
  180.           || (sys[i].st_type == SS_INPROGRESS && sys[i].locked))) {
  181.             printf("\n");
  182.             continue;
  183.         }
  184.         t = (sys[i].st_lastime +sys[i].st_retry) - curtime;
  185.         if (-t < ONEDAY*2 && sys[i].st_type != SS_FAIL)
  186.             t = 0;
  187.  
  188.         if (sys[i].st_count > MAXRECALLS)
  189.             printf("at MAX RECALLS");
  190.         else if (-t >= ONEDAY*2)
  191.             printf("%ld days ago", (long)-t/ONEDAY);
  192.         else if (t <= 0)
  193.             printf("Retry time reached");
  194.         else if (t < 60)
  195.             printf("Retry time %ld sec%s", (long)(t%60),
  196.                     (t%60)!=1? "s": "");
  197.         else
  198.             printf("Retry time %ld min%s", (long)(t/60),
  199.                 (t/60)!=1? "s": "");
  200.         if(sys[i].st_count > 1)
  201.             printf(" Count: %d\n", sys[i].st_count);
  202.         else
  203.             printf("\n");
  204.     }
  205.     if (xqtisrunning)
  206.         printf("\nUuxqt is running\n");
  207.     exit(0);
  208. }
  209.  
  210. scandir(prfx, flen, fchr, type)
  211.     char *prfx, fchr;
  212.     int type;
  213. {
  214.     register char *qname;
  215.     register int i, fnamlen, plen;
  216.     char    fnam[MAXNAMLEN+1];
  217.  
  218.     plen = strlen(prfx) + 1;
  219.     if (!work_scan((char *)NULL, prfx))
  220.         return;                /* No work of this type */
  221.  
  222.     while((qname = work_next()) != NULL) {
  223.         strcpy(fnam, &qname[plen]);
  224.         fnamlen = strlen(fnam);
  225.         if(flen > 0) {
  226.             char c;
  227.             fnamlen -= flen;
  228.             c = fnam[fnamlen];
  229.             if (islower(c))
  230.                 c = toupper(c);
  231.             if (type == DATTYPE && (c != 'S' && c != 'B')) {
  232.                 fnamlen -= 2;    /* For Honey DanBer */
  233.                 fnam[fnamlen] = NULL;
  234.             } else {
  235.                 fnam[fnamlen] = NULL;
  236.                 fnamlen = MAXBASENAME; /* yes, after = NULL*/
  237.             }
  238.         } else {
  239.             for(; fnamlen>0; --fnamlen) {
  240.                 if(fnam[fnamlen] == fchr) {
  241.                     fnam[fnamlen] = NULL;
  242.                     break;
  243.                 }
  244.             }
  245.             fnamlen = MAXBASENAME;
  246.         }
  247.         for(i=0; i<sndx; ++i) {
  248.             if(strncmp(fnam, sys[i].name, fnamlen) == SAME) {
  249.                 ++sys[i].cntr[type];
  250.                 break;
  251.             }
  252.         }
  253.         if(i == sndx) {
  254.             strcpy(sys[i].name, fnam);
  255.             ++sys[i].cntr[type];
  256.             ++sndx;
  257.         }
  258.     }
  259. }
  260.  
  261. getstst()
  262. {
  263.     register int i, csys;
  264.     register char *tp;
  265.     char    buff[128];
  266.     register char *qname;
  267.     char    *name;
  268.     char    *mungename;
  269.     register FILE *st;
  270.     int    pid;
  271.     struct stat stbuf;
  272.     long atol();
  273.  
  274.     if (work_scan((char *)NULL, "LCK"))
  275.     while ((qname = work_next()) != NULL) {
  276.         name = &qname[4];
  277.         for(csys=0; csys<sndx; ++csys) {
  278.             if(strncmp(name, sys[csys].name, SYSNSIZE)
  279.                 == SAME)
  280.                 break;
  281.         }
  282.         if(csys == sndx) {
  283.             /* remember our name */
  284.             strcpy(sys[csys].name, name);
  285.             /* We're only in it for the lock file */
  286.             sys[csys].justlck++;    
  287.             ++sndx;
  288.         }
  289.         mungename = munge_filename(qname);
  290.         if (stat(mungename, &stbuf) < 0)
  291.             sys[csys].locked = 1;
  292.         else {
  293.             sys[csys].locked = stbuf.st_mtime;
  294.             if((st = fopen(mungename, "rb")) != NULL) {
  295.                 if (1 != fread(&sys[csys].lockpid,
  296.                     sizeof (sys[csys].lockpid), 1, st))
  297.                     sys[csys].lockpid = 0;
  298.                 fclose(st);
  299.             }
  300.         }
  301.         continue;
  302.     }
  303.  
  304.     /*
  305.      * Match up locked ttys, systems, queues, etc.
  306.      *
  307.      * We go through all the sys entries, and for each one that
  308.      * has a LCK. file, chain it to all subsequent entries that are
  309.      * locked by the same pid (process).
  310.      *
  311.      * Any matching subsequent entry that is only in the table because of
  312.      * a LCK. file, gets its "printed" flag set, so it will not be
  313.      * printed on a line by itself, but instead will be printed on
  314.      * the end of a line for a system name.  If we found any system
  315.      * names while building the chain, and the first entry in the chain
  316.      * is a lock-only entry, we set *its* printed flag too.  If we
  317.      * didn't find any system names, we keep the printed flag off
  318.      * so that the whole lock-only chain will be printed on one line.
  319.      */
  320.     for(csys=0; csys<sndx; ++csys)
  321.         sys[csys].lockchain = -1;
  322.     for(csys=0; csys<sndx; ++csys) {
  323.         if (sys[csys].lockpid && sys[csys].lockchain == -1) {
  324.             int gotsystem = 0;
  325.  
  326.             sys[csys].lockchain = csys;    /* Make 1-entry chain */
  327.  
  328.             pid = sys[csys].lockpid;
  329.             for (i = csys+1; i < sndx; i++) {
  330.                 if (pid == sys[i].lockpid) {
  331.                     /* Add to chain */
  332.                     sys[i].lockchain = sys[csys].lockchain;
  333.                     sys[csys].lockchain = i;
  334.                     if (sys[i].justlck) sys[i].printed++;
  335.                     else            gotsystem++;
  336.                 }
  337.             }
  338.             if (sys[csys].justlck && gotsystem)
  339.                 sys[csys].printed++;
  340.         }
  341.     }
  342.  
  343.     /* Now check system status files */
  344.     if (work_scan((char *)NULL, "STST"))
  345.     while ((qname = work_next()) != NULL) {
  346.         for(csys=0; csys<sndx; ++csys) {
  347.             if(strncmp(&qname[5], sys[csys].name, SYSNSIZE) == SAME)
  348.                 break;
  349.         }
  350.         strcpy(sys[csys].name, &qname[5]);
  351.         if(csys == sndx) {
  352.             ++sndx;
  353.         }
  354.         if((st = fopen(munge_filename(qname), "r")) == NULL) {
  355.             sys[csys].stst[0] = '\0';
  356.             continue;
  357.         }
  358.         buff[0] = '\0';
  359.         fgets(buff, sizeof(buff), st);
  360.         fclose(st);
  361.         if(tp = rindex(buff, ' '))
  362.             *tp = NULL;        /* drop system name */
  363.         else
  364.             continue;
  365.         for(i=0, tp=buff;  i<4;  ++i, ++tp)
  366.             if((tp = index(tp, ' ')) == NULL)
  367.                 break;
  368.         if(i != 4)
  369.             continue;
  370.         strncpy(sys[csys].stst, tp, sizeof(sys[csys].stst));
  371.         tp = buff;
  372.         sys[csys].st_type = atoi(tp);
  373.         tp = index(tp+1, ' ');
  374.         sys[csys].st_count = atoi(tp+1);
  375.         tp = index(tp+1, ' ');
  376.         sys[csys].st_lastime = atol(tp+1);
  377.         tp = index(tp+1, ' ');
  378.         sys[csys].st_retry = atol(tp+1);
  379.     }
  380. }
  381.